package com.report.service.impl;

import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.operator.Equal;
import cn.zhxu.bs.util.MapUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.report.dto.request.WebMemberCollectReq;
import com.report.dto.response.WebMemberBrowseRes;
import com.report.dto.response.WebMemberCollectRes;
import com.report.entity.Collect;
import com.report.mapper.CollectMapper;
import com.report.service.ICollectService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author summer
 * @since 2024/06/25 14:54:45
 */
@Service
@Transactional(rollbackFor = Exception.class)
@AllArgsConstructor
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements ICollectService {

    private final BeanSearcher beanSearcher;

    @Override
    public Page<WebMemberCollectRes> findCollectList(WebMemberCollectReq webMemberCollectReq) {
        Page<WebMemberCollectRes> webMemberBrowseRes = new Page<>(webMemberCollectReq.getPageNum(), webMemberCollectReq.getPageSize());
        Map<String, Object> build = MapUtils.builder().page(webMemberCollectReq.getPageNum() - 1, webMemberCollectReq.getPageSize())
                .field(WebMemberCollectRes::getMemberId, webMemberCollectReq.getMemberId()).op(Equal.class).build();
        SearchResult<WebMemberCollectRes> search = beanSearcher.search(WebMemberCollectRes.class, build);
        webMemberBrowseRes.setTotal(search.getTotalCount().longValue());
        webMemberBrowseRes.setRecords(search.getDataList());
        return webMemberBrowseRes;
    }
}
